﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Windows.Controls.DataVisualization;
using CustomControls;

using System.ComponentModel;
using ActivityTracker.Library;

using Common.Library;

namespace ActivityTracker
{
    public partial class Yearly_TimespentPieChart : PhoneApplicationPage
    {
        #region Global

        Charts objCharts = new Charts();
        YearlyChart objYearlyChart = new YearlyChart();
        string year = "";
        bool newPageInstance = false;

        #endregion

        #region constructor

        public Yearly_TimespentPieChart()
        {
            InitializeComponent();
            newPageInstance = true;
        }

        #endregion

        #region Page events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string selectedYear = null;
            try
            {
                lpicYear.ItemsSource = objCharts.GetYears();
                if (NavigationContext.QueryString.TryGetValue("SelectedYearindex", out selectedYear) && newPageInstance)
                {
                    lpicYear.SelectedIndex = Convert.ToInt32(selectedYear);
                    year = lpicYear.SelectedItem.ToString();
                }
                newPageInstance = false;
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In Yearly_TimespentPieChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void lpicYear_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lpicYear.SelectedIndex == -1)
                    return;
                year = lpicYear.SelectedItem.ToString();
                LoadChart(year);
            }
            catch (Exception ex)
            {
                string mess = "Exception in YearListPicker_SelectionChanged Method In Yearly_TimespentPieChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void btnBarChart_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Charts/Yearly/Yearly_TimespentBarChart.xaml?selectedYear=" + lpicYear.SelectedIndex.ToString(), UriKind.Relative));
        }
        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Charts/Yearly/Yearly_CategoryBarChart.xaml", UriKind.Relative));
        }
        private void labeledPieSeries_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.DetailsControl.Content = ((LabeledPieSeries)sender).SelectedItem;
        }

        #endregion

        #region custom methods

        private void LoadChart(string year)
        {
            try
            {
                List<ChartProp> objChartList = objYearlyChart.LoadTimeSpentChartdata(year, ChartType.Pie);
                if (objChartList.Count > 0)
                    tblkResult.Visibility = Visibility.Collapsed;
                else
                    tblkResult.Visibility = Visibility.Visible;
                LayoutRoot.DataContext = objChartList;
                PieChart.LegendStyle = ChartLegendStyles.hidelegend();
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in LoadChart Method In Yearly_TimespentPieChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                exNew.Data.Add("year_LoadChart", year);
                throw exNew;
            }
        }

        #endregion

      
    }
}